
var MY_QRCODE_SUPERVISION = (function () {

    //获取页面统计信息
    function loadChartInfo() {
        $.ajax({
            type: "POST",
            url: "invs_home_page.do/load_chart_info",
            data: {id : '51'},
            success: function(data) {
                WeKnow.closeLoading();
                if (!WeKnow.filterAjaxResult(data)) return false; // session过期check

                if (data.errorcode == 0) {
                    var chartInfo = data.chartInfo;

                    setSelectpickerOptions(chartInfo.searchTypes);
                    //公示医疗机构数
                    drawPublicityInstitutionsCount(chartInfo.publicityInstitutions);
                    //累计公众评价数
                    drawPublicEvaluationsCount(chartInfo.publicEvaluations);
                    //累计二维码查询数
                    drawQrcodeScanningCount(chartInfo.qrcodeScanning);
                    //公众评价最高机构列表
                    drawHighestInstitutionsList(chartInfo.highestInstitutionsList);
                    //公众评价最低机构列表
                    drawLowestInstitutionsList(chartInfo.lowestInstitutionsList);
                    //公众评价满意度
                    drawSatisfacationList(chartInfo.satisfactionList);

                } else if (data.errorcode == -1 ){
                    WeKnow.errorMsg( data.errMsg );
                } else {
                    WeKnow.systemMsg('查询失败，请重新登录');
                }
            }
        });
    }

    //公示医疗机构数
    function drawPublicityInstitutionsCount(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("publicityInstitutions").innerHTML = "";

        $("#publicityInstitutions").append(
            "<span>" + data + "</span>"
        );
    }

    //累计公众评价数
    function drawPublicEvaluationsCount(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("publicEvaluations").innerHTML = "";

        $("#publicEvaluations").append(
            "<span>" + data + "</span>"
        );
    }

    //累计二维码查询数
    function drawQrcodeScanningCount(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("qrcodeScanning").innerHTML = "";

        $("#qrcodeScanning").append(
            "<span>" + data + "</span>"
        );
    }

    //公众评价最高机构列表
    function drawHighestInstitutionsList(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("highestInstitutionsList").innerHTML = "";

        for (var i=0; i < data.length; i++) {
            $("#highestInstitutionsList").append(
                "<div class='leftListItem'>"
                + "<span class='leftContentFont' style='width: 3.5rem'>" +  data[i].name + "</span>"
                + "<span class='leftContentFont' style='width: 1.5rem'>满意度" + (data[i].percent*100).toFixed(1) + "%</span></div>"
            );
        }
    }

    //公众评价最低机构列表
    function drawLowestInstitutionsList(data) {
        if(data == null || data.length == 0) {
            return;
        }

        //清空
        document.getElementById("lowestInstitutionsList").innerHTML = "";

        for (var i=0; i < data.length; i++) {
            $("#lowestInstitutionsList").append(
                "<div class='leftListItem'>"
                + "<span class='leftContentFont' style='width: 3.5rem'>" +  data[i].name + "</span>"
                + "<span class='leftContentFont' style='width: 1.5rem'>满意度" + (data[i].percent*100).toFixed(1) + "%</span></div>"
            );
        }
    }

    //公众评价满意度
    function drawSatisfacationList(data) {
        if(data == null || data.length == 0) {
            return;
        }

        var id;
        for (var i=0; i < 3; i++) {
            id = 'satisfaction' + i;
            switch (i) {
                case 0:
                    if (data.count2 != undefined) {
                        document.getElementById(id).innerText = data.count2 + "家";
                    }
                    break;
                case 1:
                    if (data.count1 != undefined) {
                        document.getElementById(id).innerText = data.count1 + "家";
                    }
                    break;
                case 2:
                    if (data.count0 != undefined) {
                        document.getElementById(id).innerText = data.count0 + "家";
                    }
                    break;
                default:
                    break;
            }
        }
    }

    function setSelectpickerOptions(data) {
        setOptionsCommon('street', data.district);
        setOptionsCommon('industry', data.industry);
        setOptionsCommon('level', data.level);
        setOptionsCommon('economic', data.economic);
        setOptionsCommon('ownership', data.ownership);
        $('.selectpicker').selectpicker('refresh');
    }

    function setOptionsCommon(name, data) {
        if (data == undefined) {
            return;
        }

        if (name == 'economic') {
            document.getElementById("economicOption0").textContent = "公立 (" + data.publicCnt + ")";
            document.getElementById("economicOption1").textContent = "民营 (" + data.privateCnt + ")";
        } else {
            var ele = $("#"+name + "Select");
            for (var i=0; i < data.length; i++) {
                var id = name + 'Option' + data[i].id;
                var element = document.getElementById(id);
                if (element) {
                    element.textContent =  data[i].name + " (" + data[i].value + ")";
                } else {
                    ele.append("<option value=" + data[i].id + " id=" + id + ">" + data[i].name + " (" + data[i].value + ")</option>");
                }
            }
        }
    }

    function getParamsSearch(searchKey, currentPage, limit) {
        var params = {};

        params.searchKey = searchKey;
        params.currentPage = currentPage;
        params.limit = limit;
        params.street = $('#streetSelect').find("option:selected").val();
        params.industry = $('#industrySelect').find("option:selected").val();
        params.economic = $('#economicSelect').find("option:selected").val();
        params.level = $('#levelSelect').find("option:selected").val();
        params.ownership = $('#ownershipSelect').find("option:selected").val();

        return params;
    }

    function searchLoad(target, searchKey, currentPage) {
        var limit = 10;
        var $that = target;
        var params = getParamsSearch(searchKey, currentPage, limit);
        $.ajax({
            type: "POST",
            url: "invs_home_page.do/dispaly_search_login",
            data: params,
            success: function(data) {
                WeKnow.closeLoading();
                if (!WeKnow.filterAjaxResult(data)) return false; // session过期check

                if (data.errorcode == 0) {
                    var institutionList = data.institutionList;
                    var total = data.total;
                    $('#searchList').empty();
                    if (institutionList == null || institutionList.length == 0 ) {
                        //
                    } else {

                        $(institutionList).each(function (idx, g) {
                            $((function () {
                                var e = '';
                                e += '<tr class="search-tr">';
                                e += '<td class="search-td">' + g.name + '</td>';
                                e += '<td style="display:none">' + g.id + '</td>';
                                e += '</tr>';
                                return e;
                            })()).appendTo($('#searchList')).on('click', function () {
                                $('input#insitutionNameSearch').val(g.name);
                                $('button#institutionDetails').attr("data-institutionname", g.name);
                                $('button#institutionDetails').attr("data-institutionid", g.id);

                                //判断状态是否为全屏
                                var screenState = $("#full_scr").is(":hidden") ? 'fullScreen' : 'exitFullScreen';
                                WeKnow.submit(
                                    'invs_institution_details.do',
                                    {
                                        screenState: screenState,
                                        pageId: "qrcodeSupervision",
                                        institutionName: g.name,
                                        institutionId: g.id,
                                    }
                                );
                            });
                        });
                    }

                    // $('.selectpicker').selectpicker('refresh');

                    if (total > limit) {
                        $('div.layui-table-page').show();
                    } else {
                        $('div.layui-table-page').hide();
                    }

                    layui.use('laypage', function(){
                        var laypage = layui.laypage;
                        //执行一个laypage实例
                        laypage.render({
                            elem: 'searchPagination',
                            count: total,
                            limit: limit,
                            curr: currentPage,
                            // theme: 'rgb(46,76,255)',
                            jump: function (obj, first) { //obj为当前页的属性和方法，第一次加载first为true
                                //非首次加载 do something
                                if (!first) {
                                    //清空以前加载的数据
                                    $('#searchList').empty();
                                    //调用加载函数加载数据
                                    MY_QRCODE_SUPERVISION.searchLoad($that, searchKey, obj.curr);
                                }
                            }
                        });
                    });

                } else if (data.errorcode == -1 ) {
                    WeKnow.errorMsg( data.errMsg );
                } else {
                    WeKnow.systemMsg('查询失败，请重新登录');
                }
            }
        });
    }

    return {
        loadChartInfo:loadChartInfo,
        searchLoad:searchLoad,
    }
})();

$(document).ready(function() {

    MY_QRCODE_SUPERVISION.loadChartInfo();
    setInterval(function(){ MY_QRCODE_SUPERVISION.loadChartInfo(); }, 10000);

    $('#economicSelect, #ownershipSelect, #levelSelect').selectpicker({
        width: MY_HOME_PAGE_COMMON.fontChart(1.5)
    });

    $('#industrySelect, #streetSelect').selectpicker({
        width: MY_HOME_PAGE_COMMON.fontChart(2)
    });

    $('.selectpicker').on('change',function(){
        $("input#insitutionNameSearch").trigger("click");
    });

    $("#institutionDetails").on('click',function () {
        $("input#insitutionNameSearch").trigger("click");
    });

    var autoSearch;
    $('input#insitutionNameSearch').on('input click', function () {

        if (document.getElementById("searchMenu").style.display == 'none') {
            document.getElementById("searchMenu").style.display = 'inline';
        }

        if ($("#searchMenu").is(":hidden")) {
            $("#searchMenu").show();
            $(document).one("click",function () {
                $("#searchMenu").hide();
            });
            e.stopPropagation();
        }

        if (autoSearch) clearTimeout(autoSearch);

        var $that = $(this);
        var searchKey = $.trim($that.val());
        // if (searchKey.length) {
        autoSearch = setTimeout(MY_QRCODE_SUPERVISION.searchLoad($that, searchKey, 1), 500);
        // }
    });

    $(".mainbox").on('click', function(e) {
        if (document.getElementById("searchMenu").style.display == 'inline') {
            document.getElementById("searchMenu").style.display = 'none';
        }
    });
});

